const axios = require('axios'); 
import { Message } from 'element-ui';
import router from '../../router/index'							// 生成一个axios实例
import {host} from '../config/config'
import Vue from "vue";
axios.defaults.baseURL = host+'api/'; 		// 设置请求后端的URL地址
axios.defaults.timeout = 50000; 								// axios请求超时时间
axios.defaults.headers['Content-Type'] = 'application/json'; 	// axios发送数据时使用json格式
axios.defaults.transformRequest = data => JSON.stringify(data); // 发送数据前进行json格式化
 axios.defaults.withCredentials=false
axios.interceptors.request.use(config => {
	var token= sessionStorage.getItem("token")
	config.headers.Authorization = "Bearer "+token;
	return config;
}, error => {
	return Promise.reject(error);
});
axios.interceptors.response.use(response => {
	if(response.data.statusCode==401){
		Message.error('登录过期，请重新登录');
		Vue.prototype.connectionSignalR=null;
		router.push({ path: '../Login' });
		return;
	}
	return response.data;
}, error => {
	if(error.response.data.statusCode==401){
		Vue.prototype.connectionSignalR=null;
		Message.error('登录过期，请重新登录');
		router.push({ path: '../Login' });
		return;
	}else if(error.response.data.statusCode>=400||error.response.data.statusCode<500){
		Message.error(error.response.data.message);
	}
	else{
		Message.error(error.response.data.value.message);
	}
	return error.response.data
});
export function get(url, params) {
	return new Promise((resolve, reject) => {
		axios.get(url, { params }).then(res => {
			resolve(res)
		}).catch(err => {
			reject(err)
		})
	})
}
export function Download(url, data) {
	return new Promise((resolve, reject) => {
		axios({
			headers: { "Content-disposition": "attachment; filename=rzjl.xlsx", "Content-Type": "application/msexcel", },
			responseType: "blob",
			method: "GET",
			url: url,
			params: { data },
		}).then(res => {
			if (res) downloadFile(res, Date.parse(new Date()))
			resolve(true)
		}).catch(err => {
			reject(err)
		})
	})
}
export function Downloads(urls){
	return new Promise((resolve,reject)=>{
		axios({
			responseType:'blob',
			method:'GET',
			url:urls,
			headers:{}
		}).then(res=>{
			if (res){
				console.log(res);
				var blob = new Blob([res], { type: 'application/octet-stream' })
				var url = window.URL.createObjectURL(blob);
				var aLink = document.createElement("a");
				aLink.style.display = "none";
				aLink.href = url;
				aLink.setAttribute("download", urls.split("file/")[1]);
				document.body.appendChild(aLink);
				aLink.click();
				document.body.removeChild(aLink); //下载完成移除元素
				window.URL.revokeObjectURL(url); //释放掉blob对象
			}
			resolve(true)
		})
		.catch(err=>{reject(err)})
	})
}
function downloadFile(data, name) {
	if (!data) return null;
	var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
	var url = window.URL.createObjectURL(blob);
	var aLink = document.createElement("a");
	aLink.style.display = "none";
	aLink.href = url;
	aLink.setAttribute("download", name + ".xls");
	document.body.appendChild(aLink);
	aLink.click();
	document.body.removeChild(aLink); //下载完成移除元素
	window.URL.revokeObjectURL(url); //释放掉blob对象
}
export function post(url, params) {
	return new Promise((resolve, reject) => {
		axios.post(url, params).then((res) => {
			resolve(res)
		}).catch((err) => {
			// debugger
			reject(err)
		})
	})
}
export function put(url, params) {
	return new Promise((resolve, reject) => {
		axios.put(url, params).then((res) => {
			resolve(res)
		}).catch((err) => {
			// debugger
			reject(err)
		})
	})
}
export function del(url, params) {
	return new Promise((resolve, reject) => {
		axios.delete(url, { data: params }).then((res) => {
			resolve(res)
		}).catch((err) => {
			// debugger
			reject(err)
		})
	})
}

export function uploading(url,data){
	return new Promise((resolve,reject)=>{
		axios({
			method: 'post',
			url: url,
			data:data,
			responseType: 'blob',
			headers: {'Content-Type':'multipart/form-data'}
		})
		.then(res => {
			resolve(res)
		})
		.catch(err => {
			reject(err)
		})
	})
}
export default axios;